package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.controller.dto.UserDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.PasswordDto;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @Author Harlan
 * @Date 2023/9/12 18:26
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 更新当前登录用户的密码密码
     * @param request
     * @param params
     * @return
     */
    @PutMapping("/password")
    public Result editPassword(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        return userService.editPasswordByUserId(
                Integer.valueOf(request.getAttribute("userId").toString()),
                params.get("password").toString()) ?
                Result.buildSuccess() :
                Result.buildFail();
    }

    /**
     * 编辑用户
     * @param userDto
     * @return
     */
    @PutMapping
    public Result edit(@RequestBody UserDto userDto) {
        return userService.editOne(userDto) ? Result.buildSuccess() : Result.buildFail();
    }

    /**
     * 新增用户
     * @param userDto
     * @return
     */
    @PostMapping
    public Result save(@RequestBody UserDto userDto) {
        return userService.saveOne(userDto) ? Result.buildSuccess() : Result.buildFail();
    }

    /**
     * 根据用户ID获取该用户下所有的角色ID
     * @param id
     * @return
     */
    @GetMapping("/roles")
    public Result getRoleIdsByUserId(Integer id) {
        return Result.buildSuccess(userService.getRoleIdsByUserId(id));
    }


    /**
     * 分页查询用户信息
     * @param queryPageBean
     * @return
     */
    @GetMapping
    public Result getUsers(QueryPageBean queryPageBean) {
        Page<User> userPage = userService.findPage(queryPageBean);
        return Result.buildSuccess(userPage);
    }

    /**
     * 获取用户的信息
     * @param request
     * @return
     */
    @GetMapping("/info")
    public Result info(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, userId));
        return Result.buildSuccess(user);
    }

    /**
     * 登录接口
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        user = userService.login(user.getUsername(), user.getPassword());
        if (!Objects.isNull(user)) {
            // 生成令牌
            String token = JWTUtil.generateJWT(user);
            return Result.buildSuccess(token);
        }
        return Result.buildFail();
    }
}
